﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace BlogDemo.Common
{
    public class EncryptionHelper
    {
        static readonly String _EncryptionKey = "eid729";

        public static string Encrypt(string clearText)
        {
            byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
            using (Aes encryptor = Aes.Create())
            {
                //Rfc2898DeriveBytes.
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(_EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(clearBytes, 0, clearBytes.Length);
                        cs.Close();
                    }
                    clearText = Convert.ToBase64String(ms.ToArray());
                }
            }
            return clearText;
        }
        public static string Decrypt(string cipherText)
        {
            cipherText = cipherText.Replace(" ", "+");
            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(_EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(cipherBytes, 0, cipherBytes.Length);
                        cs.Close();
                    }
                    cipherText = Encoding.Unicode.GetString(ms.ToArray());
                }
            }
            return cipherText;
        }
        /*  private enum CryptoFunction
          {
              Encrypt, Decrypt
          }
          private readonly IOptions<AESKeys> _aesKeys;
          public EncryptionHelper(IOptions<AESKeys> aesKeys)
          {
              _aesKeys = aesKeys;
          }

          /// <summary>
          /// Tries to decrypt a AES256 encrypted string.
          /// If the string is not encrypted or if it can't be decrypted, return the original string.
          /// </summary>
          /// <param name="encrypted"></param>
          /// <returns></returns>
          public string TryDecryptAES256(string encrypted) => AES256(CryptoFunction.Decrypt, encrypted);

          public string EncryptAES256(string toEncrypt) => AES256(CryptoFunction.Encrypt, toEncrypt);

          private string AES256(CryptoFunction cryptoFunction, string input)
          {
              try
              {
                  if (string.IsNullOrEmpty(_aesKeys.Value.Key) || string.IsNullOrEmpty(_aesKeys.Value.IV))
                      return input;

                  using (var aes = Aes.Create())
                  {
                      ICryptoTransform cryptoTransform;
                      switch (cryptoFunction)
                      {
                          case CryptoFunction.Encrypt:
                              cryptoTransform = aes.CreateEncryptor(Convert.FromBase64String(_aesKeys.Value.Key), Convert.FromBase64String(_aesKeys.Value.IV));
                              break;
                          case CryptoFunction.Decrypt:
                              cryptoTransform = aes.CreateDecryptor(Convert.FromBase64String(_aesKeys.Value.Key), Convert.FromBase64String(_aesKeys.Value.IV));
                              break;
                          default:
                              cryptoTransform = null;
                              break;
                      }

                      using (cryptoTransform)
                      {
                          var inputBytes = System.Text.Encoding.UTF8.GetBytes(input);

                          var transformedBytes = cryptoTransform.TransformFinalBlock(inputBytes, 0, inputBytes.Length);
                          return System.Text.Encoding.UTF8.GetString(transformedBytes);
                      }
                  }
              }
              catch (Exception ex)
              {
                  return input;
              }
          }*/
    }
}